import pyautogui, os, threading, pyperclip, time, tkinter, webbrowser
from tkinter.messagebox import showinfo
from tkinter import *
from tkinter import ttk


class MsgeBomb:
    frequency_ = 0.01  # 轰炸频率
    _flag = 0  # 轰炸控制变量

    # 初始函数，创建窗体
    def __init__(self):

        
        self.win = Tk()
        self.win.title('消息轰炸机')
        self.win.geometry('400x320')
        self.win.resizable(width=False, height=False)
        self.win.iconbitmap(default='./lib/icon/logo.png')
        self.file = os.listdir('./lib')
        self.value = [v.replace('.txt', '') for v in self.file if v.endswith('.txt')]
        self.value = tuple(self.value)
        # 设置默认值，即默认下拉框中的内容,索引从0开始

        mebubar1 = Menu(self.win, tearoff=0)
      

        ks_dy = lambda: webbrowser.open(_ks_dy)
        mebubar1.add_command(label="关于", command=about)
        
        mebubar1.add_command(label="退出", command=self.win.quit)

        mebubar = Menu(self.win, tearoff=0)
        mebubar.add_cascade(label="功能", menu=mebubar1)
        self.win.config(menu=mebubar)

        self.ddb_default_L = Label(self.win, text='词库选择：', font=('Arial', 13), fg='blue')
        self.ddb_default = ttk.Combobox(self.win)
        self.ddb_default['value'] = self.value
        self.ddb_default['state'] = 'readonly'
        self.ddb_default.current(0)
        # 获得选中的文本
        self.text = self.ddb_default.get()

        self.btn_start = Button(self.win, text='开始轰炸', width=16, bd=5, font=('黑体', 10, 'bold'),
                                command=lambda: self.thread_start(self.start_On))
        self.btn_stop = Button(self.win, text='停止轰炸', width=16, bd=5, font=('黑体', 10, 'bold'), command=self.stop_Off)

        self.textvar = tkinter.StringVar()
        # 统计轰炸次数控件
        self.count = Label(self.win, font=('黑体', 13, 'bold'), fg='red', textvariable=self.textvar)
        # 下拉框颜色
        combostyle = ttk.Style()
        combostyle.theme_create('combostyle', parent='alt',
                                settings={'TCombobox':
                                    {'configure':
                                        {
                                            'foreground': 'blue',  # 前景色
                                            # 'selectbackground': 'blue',  # 选择后的背景颜色
                                            'fieldbackground': 'pink',  # 下拉框颜色
                                            'background': 'pink',  # 下拉按钮颜色
                                        }}}
                                )
        combostyle.theme_use('combostyle')

        '''grid布局'''

        self.ddb_default_L.grid(row=1, column=1, sticky='E', padx=1, pady=50)
        self.ddb_default.grid(row=1, column=2, sticky='NW', padx=2, pady=50)

        self.btn_start.grid(row=2, column=1, sticky='E', padx=5, pady=10)
        self.btn_stop.grid(row=2, column=2, sticky='NW', padx=3, pady=10)
        self.count.grid(row=3, column=1, sticky='NW', padx=3, pady=4)

        self.win.mainloop()

    def jion_qun(self):
        # 加群链接
        _qun_url = 'https://qm.qq.com/cgi-bin/qm/qr?k=puepc8hF5YTBgbhNU6E2lPgTnOl1vg38&jump_from=webapi'
        showinfo('加群提醒', '加群前需关注抖音/快手，否则不予通过！')
        webbrowser.open(_qun_url)

    # 发送消息函数
    def sendMessage(self, content):
        pyperclip.copy(content)
        pyautogui.hotkey('ctrl', 'v')
        pyautogui.press('enter')

    # 停止轰炸
    def stop_Off(self):
        self._flag = 1  # 将状态改为1，停止轰炸
        self.textvar.set(f'已停止轰炸')

    # 开始轰炸函数
    def start_On(self):
        self._flag = 0
        self.textvar.set(f'2秒后将开始轰炸')
        time.sleep(2)
        text = f'./lib/{self.ddb_default.get()}.txt'
        file = open(text, 'r', encoding='utf-8')
        content = file.read().split('\n')
        for i in range(len(content)):
            if self._flag == 1:
                break
            else:
                self.textvar.set(f'已轰炸{i + 1}次')
                self.sendMessage(content[i])
                time.sleep(self.frequency_)
        self.textvar.set(f'轰炸完毕')

    def thread_start(self, func, *args):
        t = threading.Thread(target=func, args=args)
        t.setDaemon(True)
        t.start()


win = MsgeBomb()
